import { watch, reactive, ref, computed } from "vue"
import {
  getPricePieData,
  getAreaPieData,
  getNaturePieData,
  getIndustryPieData,
  getCompanyItemStatisticsByAmount,
  getCompanyItemStatisticsByLevel,
  getCompanyItemStatisticsByOrg,
  getCompanyItemStatisticsByYear
} from "../../service/article/"
import moment from "moment"

export const useAnalysisHook = () => {
  interface PieInfoType {
    title: string
    desc: string
  }

  const pieData = ref<any[]>([])
  const pieInfo = ref<PieInfoType>({
    title: "",
    desc: ""
  })

  const detailInfo = reactive<any>({
    id: "",
    type: ""
  })

  const url = new URL(location.href);
  const searchParams = url.searchParams;

  //监听路由query变化
  watch(
    () => searchParams,
    (newVal) => {
      detailInfo.id = newVal.get("id");
      detailInfo.type = newVal.get("type");
    },
    { deep: true, immediate: true }
  );

  // 项目批次数据
  const batchs = ref([
    { dictLabel: "2024", dictValue: "2024" },
    { dictLabel: "2023", dictValue: "2023" },
    { dictLabel: "2022", dictValue: "2022" },
    { dictLabel: "2021", dictValue: "2021" },
    { dictLabel: "2020", dictValue: "2020" },
    { dictLabel: "自定义", dictValue: "0" }
  ])

  // 查询表单
  const form = reactive<any>({
    policyId: detailInfo.id,
    year2: moment().format("YYYY"),
    province: 19,
  })

  const loading = ref(false)

  // 是否显示筛选
  const showFilterBar = computed(() => {
    if (detailInfo.type == "1" || detailInfo.type == "2" || detailInfo.type == "3" || detailInfo.type == "4") {
      return true
    } else {
      return false
    }
  })

  // 初始化页面数据
  const init = async () => {
    const params = { ...form }
    if (form.year == '0') {
      params.year = params.year2
    }
    delete params.year2

    loading.value = true
    pieData.value = []
    let result: any = []
    switch (detailInfo.type) {
      // 请求金额饼
      case "1":
        pieInfo.value = {
          title: "金额分析",
          desc: "占位内容"
        }
        result = await getPricePieData(params)
        break;

      // 地区饼
      case "2":
        pieInfo.value = {
          title: "地区分析",
          desc: "占位内容"
        }
        result = await getAreaPieData(params)
        break;

      // 性质饼
      case "3":
        pieInfo.value = {
          title: "性质分析",
          desc: "占位内容"
        }
        result = await getNaturePieData(params)
        break;

      // 行业饼
      case "4":
        pieInfo.value = {
          title: "行业分析",
          desc: "占位内容"
        }
        result = await getIndustryPieData(params)
        break;

      case "5":
        pieInfo.value = {
          title: "等级分析",
          desc: "占位内容"
        }
        result = await getCompanyItemStatisticsByLevel(detailInfo.id)
        break;

      case "6":
        pieInfo.value = {
          title: "部门分析",
          desc: "占位内容"
        }
        result = await getCompanyItemStatisticsByOrg(detailInfo.id)
        break;

      case "7":
        pieInfo.value = {
          title: "批次分析",
          desc: "占位内容"
        }
        result = await getCompanyItemStatisticsByYear(detailInfo.id)
        break;

      case "8":
        pieInfo.value = {
          title: "金额分析",
          desc: "占位内容"
        }
        result = await getCompanyItemStatisticsByAmount(detailInfo.id)
        break;
    }
    loading.value = false

    pieData.value.push(result.data)
  }

  init()

  // 选择方法
  const checkTag = (val: string) => {
    if (form.year == val) {
      form.year = ""
    } else {
      form.year = val
    }

    console.log(form.year == val);

    init()
  }

  const yearChange = (val: any) => {
    init()
  }

  const itemLoading = ref(false)

  // 请求子饼数据
  const querySubPieData = async (val: any, idx: any) => {
    // 判断当前点击的层级以及数据层数
    if (pieData.value.length - 1 > idx || idx == 3 || detailInfo.type == "1" || !val.id) return false;

    itemLoading.value = true

    let result: any = []

    // 请求参数
    const params = { ...form }

    if (form.year == '0') {
      params.year = params.year2
    }

    delete params.year2

    switch (detailInfo.type) {
      // 地区饼数据
      case "2":
        result = await getAreaPieData({ ...params, areaId: val.id })
        break;
      case "3":
        result = await getNaturePieData({ ...params, natureId: val.id })
        break;
      case "4":
        result = await getIndustryPieData({ ...params, industryId: val.id, level: idx + 2 })
        break;
      case "5":
        result = await getIndustryPieData({ ...params, industryId: val.id, level: idx + 2 })
        break;
      case "6":
        result = await getIndustryPieData({ ...params, industryId: val.id, level: idx + 2 })
        break;
      case "7":
        result = await getIndustryPieData({ ...params, industryId: val.id, level: idx + 2 })
        break;
      case "8":
        result = await getIndustryPieData({ ...params, industryId: val.id, level: idx + 2 })
        break;
    }

    pieData.value.push(result.data)
    itemLoading.value = false
  }

  // 关闭饼图
  const closePieByIndex = (index: number) => {
    pieData.value.pop()
  }

  return { detailInfo, pieData, pieInfo, querySubPieData, closePieByIndex, batchs, form, checkTag, yearChange, loading, itemLoading, showFilterBar }
}
